/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.basics.editor.gui.dialog;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;

import com.genia.toolbox.basics.editor.gui.AbstractGUI;
import com.genia.toolbox.basics.editor.model.bean.InitialSettings;

/**
 * Abstract settings dialog.
 * 
 * @param <G>
 *          The gui.
 * @param <I>
 *          The settings.
 */
@SuppressWarnings("serial")
public abstract class AbstractSettingsDialog<G extends AbstractGUI<?, ?, ?, ?, ?, I>, I extends InitialSettings>
    extends JDialog
    implements ActionListener
{

  /**
   * The application gui.
   */
  private G gui;

  /**
   * The cancel button.
   */
  private JButton buttonCancel = null;

  /**
   * The validation button.
   */
  private JButton buttonOK = null;

  /**
   * The current settings.
   */
  private I settings = null;



  /**
   * Constructor.
   * 
   * @param gui
   *          The gui.
   */
  public AbstractSettingsDialog(G gui)
  {
    super(gui.getFrame(), true);
    this.gui = gui;
  }



  /**
   * Initialise the dialog.
   */
  public void initialise()
  {
    this.setTitle(this.gui.getController().notifyTranslation(this.getTitleKey()));

    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    int width = this.getWidth();
    int height = this.getHeight();
    this.setBounds((dim.getSize().width - width) / 2, (dim.getSize().height - height) / 2, width, height);

    GridBagLayout layout = new GridBagLayout();
    GridBagConstraints constraints = new GridBagConstraints();
    this.getContentPane().setLayout(layout);

    constraints.gridx = 1;
    constraints.gridy = 1;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 1;
    constraints.weighty = 1;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(5, 5, 5, 5);
    this.getContentPane().add(this.createMainPanel(), constraints);

    constraints.gridx = 1;
    constraints.gridy = 2;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 1;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.NONE;
    constraints.insets = new Insets(0, 5, 5, 5);
    constraints.anchor = GridBagConstraints.EAST;
    this.getContentPane().add(this.createButtonPanel(), constraints);
  }



  /**
   * Get the title key.
   * 
   * @return the title key.
   */
  public abstract String getTitleKey();



  /**
   * Get the ok button key.
   * 
   * @return the ok button key.
   */
  public abstract String getOkKey();



  /**
   * Get the ok cancel key.
   * 
   * @return the ok cancel key.
   */
  public abstract String getCancelKey();



  /**
   * Get the width.
   * 
   * @return the width.
   */
  @Override
  public abstract int getWidth();



  /**
   * Get the height.
   * 
   * @return the height.
   */
  @Override
  public abstract int getHeight();



  /**
   * Create the main panel.
   * 
   * @return The main panel.
   */
  public abstract JPanel createMainPanel();



  /**
   * Process the event of an user clicking on the OK button.
   */
  public abstract void doOk();



  /**
   * Create the panel containing the buttons.
   * 
   * @return The button panel.
   */
  public JPanel createButtonPanel()
  {
    JPanel panel = new JPanel();

    GridBagLayout layout = new GridBagLayout();
    GridBagConstraints constraints = new GridBagConstraints();
    panel.setLayout(layout);

    this.buttonCancel = new JButton(this.gui.getController().notifyTranslation(this.getCancelKey()));
    this.buttonCancel.addActionListener(this);
    constraints.gridx = 1;
    constraints.gridy = 1;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 1;
    constraints.weighty = 1;
    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.insets = new Insets(0, 0, 0, 0);
    panel.add(this.buttonCancel, constraints);

    this.buttonOK = new JButton(this.gui.getController().notifyTranslation(this.getOkKey()));
    this.buttonOK.addActionListener(this);
    constraints.gridx = 2;
    constraints.gridy = 1;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 1;
    constraints.weighty = 1;
    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.insets = new Insets(0, 5, 0, 0);
    panel.add(this.buttonOK, constraints);

    return panel;

  }



  /**
   * Handle the action events.
   * 
   * @param e
   *          the action event.
   */
  public void actionPerformed(ActionEvent e)
  {
    Object source = e.getSource();
    if (source.equals(this.buttonCancel)) {
      this.dispose();
    }
    else if (source.equals(this.buttonOK)) {
      this.doOk();
    }
  }



  /**
   * Get the initial settings.
   * 
   * @return The initial settings.
   */
  public I getSettings()
  {
    return this.settings;
  }



  /**
   * Set the initial settings.
   * 
   * @param settings
   *          The initial settings.
   */
  public void setSettings(I settings)
  {
    this.settings = settings;
  }



  /**
   * Get the gui.
   * 
   * @return the gui.
   */
  public G getGui()
  {
    return this.gui;
  }

}
